package com.cntaiping.royal.bass.core.controller;

import com.cntaiping.royal.bass.common.util.UtilTools;
import com.cntaiping.royal.bass.core.entity.JClassRecord;
import com.cntaiping.royal.bass.core.entity.JCours;
import com.cntaiping.royal.bass.core.entity.ModelData;
import com.cntaiping.royal.bass.core.entity.RequestPO.JClassRecordDTO;
import com.cntaiping.royal.bass.core.service.ClassRecordService;
import com.cntaiping.royal.bass.core.service.CourseService1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService1 courseService;

    @Autowired
    private ClassRecordService classRecordService;

    @RequestMapping(value = "/queryByNew", produces = {"application/json;charset=UTF-8"})
    public Object queryJcours(String systemId) {
        Map map = new HashMap<String, String>();
        map.put("systemId", systemId);

        ModelData modelData = new ModelData();

        JCours jcours = courseService.findByOne(map);
        if (jcours != null) {
            jcours.setCourseNumberStr(UtilTools.toChinese(String.valueOf(jcours.getCourseNumber())));
        }

        if (jcours != null) {
            modelData.setMsg("查询成功");
        } else {
            modelData.setMsg("查询结果为空");
        }

        modelData.setjData(jcours);
        modelData.setSuccess(true);

        return modelData;
    }

    @RequestMapping(value = "/detail", produces = {"application/json;charset=UTF-8"})
    public Object detail(String systemId, String CId, String openId) {
        Map map = new HashMap<String, String>();
        map.put("systemId", systemId);
        map.put("courseId", CId);
        map.put("openid", openId);

        ModelData modelData = new ModelData();

        JCours jcours = courseService.findByOne(map);
        if (jcours != null) {
            jcours.setCourseNumberStr(UtilTools.toChinese(String.valueOf(jcours.getCourseNumber())));
        }

        List<JClassRecord> classRecords = classRecordService.selectRecords(map);

        if (classRecords != null) {
            jcours.setIsPunch(classRecords.size() + "");
        } else {
            jcours.setIsPunch("0");
        }

        if (jcours != null) {
            modelData.setMsg("查询成功");
        } else {
            modelData.setMsg("查询结果为空");
        }

        modelData.setjData(jcours);
        modelData.setSuccess(true);

        return modelData;
    }

    @PostMapping(value = "/queryList", produces = {"application/json;charset=UTF-8"})
    public Object queryList(@RequestBody JClassRecordDTO jClassRecordDTO) {
        Map map = new HashMap<String, String>();
        map.put("systemId", jClassRecordDTO.getSystemId());
        map.put("openId", jClassRecordDTO.getOpenid());

        ModelData modelData = new ModelData();

        List<HashMap<String, Object>> jcours = courseService.findList(map);

        String signInNum = courseService.querySignIn(map);


        if (!CollectionUtils.isEmpty(jcours)) {
            for (HashMap<String, Object> conrse : jcours) {
                String courseNumber = conrse.get("courseNumber") + "";
                if (conrse != null && courseNumber.equals(signInNum)) {
                    conrse.put("status", "-1");
                }
            }
        }

        if (jcours != null) {
            modelData.setMsg("查询成功");
        } else {
            modelData.setMsg("查询结果为空");
        }

        modelData.setjData(jcours);
        modelData.setSuccess(true);


        return modelData;
    }

}
